﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinSat
{
    public partial class frmEvraklar : Form
    {
        public frmEvraklar()
        {
            InitializeComponent();
        }

        bool KlavyeAcik = false;
        private void btnKlavye_Click(object sender, EventArgs e)
        {
            if (KlavyeAcik)
            {
                KlavyeAcik = false;
                Sip.Hide();
            }
            else
            {
                KlavyeAcik = true;
                Sip.Show();
            }
        }

        private void btnKapat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmEvraklar_Load(object sender, EventArgs e)
        {
            try
            {
                using (Query q = new Query())
                {
                    q.Add(string.Format("SELECT     t1.order_series AS Seri, t1.order_no AS Sıra, t2.name AS [Cari Ünvanı],t1.customer_code,(case when t1.terminal = 0 then 'Aktarılmadı' else 'Aktarıldı' end) as Durum FROM ORDER_DETAILS as t1 WITH (NOLOCK) INNER JOIN CUSTOMERS as t2 WITH (NOLOCK) ON t1.customer_code = t2.code WHERE     (t1.supplier = '{0}') GROUP BY t1.order_series, t1.order_no, t2.name,t1.customer_code,t1.terminal", Program.Session[Settings.Tedarikci]));
                    dataGrid1.VeriKaynagi = q.Fill();
                    dataGrid1.Columns[3].Width = -1;
                    dataGrid1.Columns[4].Width = 70;
                }
            }
            catch (Exception ex)
            {
                ExceptionHandling.Hand(ex);
            }
        }

        private void dataGrid1_DoubleClick(object sender, EventArgs e)
        {
            Sec();
        }

        private void Sec()
        {
            if (dataGrid1.RowCount > 0)
            {
                if (dataGrid1.CurrentRowIndex > -1)
                {
                    string seri = dataGrid1[dataGrid1.CurrentRowIndex, 0].ToString();
                    int sira = Cevir.Parse<int>(dataGrid1[dataGrid1.CurrentRowIndex, 1]);
                    string cari = dataGrid1[dataGrid1.CurrentRowIndex, 3].ToString();
                    frmSiparis sip = new frmSiparis();
                    sip.EvrakSeri = seri;
                    sip.EvrakSira = sira;
                    sip.Cari = new Cari() { Kodu = cari, Adi = dataGrid1[dataGrid1.CurrentRowIndex, 2].ToString() };
                    sip.EskiEvrak = true;
                    sip.Show();
                }
            }
        }

        private void btnDegistir_Click(object sender, EventArgs e)
        {
            try
            {
                Sec();
            }
            catch (Exception ex)
            {
                ExceptionHandling.Hand(ex);
            }
        }

        private void btnSil_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult res = MessageBox.Show("Silmek istediğinize eminmisiniz ?", "Uyarı", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (res == DialogResult.Yes)
                {
                    if (dataGrid1.RowCount > 0)
                    {
                        if (dataGrid1.CurrentRowIndex > -1)
                        {
                            string seri = dataGrid1[dataGrid1.CurrentRowIndex, 0].ToString();
                            int sira = Cevir.Parse<int>(dataGrid1[dataGrid1.CurrentRowIndex, 1]);

                            using (Query q = new Query())
                            {
                                q.Add(string.Format("DELETE FROM ORDER_DETAILS WHERE (order_series = '{0}') AND (order_no = {1}) ", seri, sira));
                                q.Exec();
                            }
                            dataGrid1.RemoveRow(dataGrid1.CurrentRowIndex);
                            dataGrid1.Refresh();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionHandling.Hand(ex);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGrid1.RowCount > 0)
                {
                    if (dataGrid1.CurrentRowIndex > -1)
                    {
                        string seri = dataGrid1[dataGrid1.CurrentRowIndex, 0].ToString();
                        int sira = Cevir.Parse<int>(dataGrid1[dataGrid1.CurrentRowIndex, 1]);

                        using (Query q = new Query())
                        {
                            q.Add(string.Format("UPDATE ORDER_DETAILS SET printed=0,terminal=0 WHERE (order_series = '{0}') AND (order_no = {1}) ", seri, sira));
                            q.Exec();
                        }
                        MessageBox.Show("Tamamlandı");
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionHandling.Hand(ex);
            }
        }
    }
}